<template>
	<view>
		<view class="subsection-box">
			<view :class="selectSubsection == item ? 'item active' : 'item'" @click="handleSelectSubsection(item,index)"
				v-for="(item,index) in list" :key="index">
				{{getStr('my.bag.' + item)}}
			</view>
		</view>
		<!-- <view class="tab-box">
			<view :class="selectTab == item ? 'item active' : 'item'" @click="selectTab = item"
				v-for="(item,index) in tabList" :key="index">
				{{item}}
			</view>(
		</view> -->

		<view class="list-box">
			<view :class="`item good-bg1 item${item.cardId}`" v-for="(item,index) in goods" @click="()=>handleGoods(item)">
				<view class="start-box" v-if="item.wakeStatus == 'not'">
					<text v-if="item.turnoverNumber >= 12 ">{{getStr('ReturnStarPool')}}</text>
					<text v-if="item.turnoverNumber < 12 && item.cardStatus == 'normal'">{{getStr('salability')}}</text>
					<text v-if="item.turnoverNumber < 12 && item.cardStatus == 'selling'">{{getStr('onSale')}}</text>
					<text v-if="item.turnoverNumber < 12 && item.cardStatus == 'destroy'"></text>
				</view>
				<view class="start-box" v-if="item.wakeStatus == 'yes' && item.remainingTime">
					<image src="/static/images1/starPond/time.png" style="width: 15rpx; height: 15rpx;margin-right: 6rpx;">
					</image>
					<up-count-down :time="item.remainingTime" format="HH:mm:ss"></up-count-down>
				</view>
				<view class="start-box" v-if="item.wakeStatus == 'yes' && !item.remainingTime">
					<text v-if="item.turnoverNumber >= 12 ">{{getStr('invalidCard')}}</text>
				</view>
				<view class="item-info">
					<image :src="'/static/xz/thumb/'+item.cardId+'.png'"
						style="width: 146rpx; height: 225rpx;border-radius: 12rpx;margin: 0 auto;"></image>
					<view class="title" v-if="locale == 'zh'">{{item.zhName}}</view>
					<view class="title" v-if="locale == 'en'">{{item.enName}}</view>
					<view class="hour-box">
						<text>{{item.currentPrice}}</text>
						<image src="/static/images1/star-bb.png" style="width: 18rpx; height: 18rpx;margin: 0 6rpx 0 2rpx;"></image>
					</view>
					<view class="bottom-box">
						<view>
							<text>{{getStr('trade.issuePrice')}}:{{item.releasePrice}}</text>
							<image src="/static/images1/star-bb.png" style="width: 18rpx; height: 18rpx;margin: 0 6rpx 0 2rpx;">
							</image>
						</view>
						<view>
							<text>{{getStr('trade.lastPrice')}}:{{item.lastPrice}}</text>
							<image src="/static/images1/star-bb.png" style="width: 18rpx; height: 18rpx;margin: 0 6rpx 0 2rpx;">
							</image>
						</view>
					</view>
				</view>
				<view class="divider-box"><up-divider text="" lineColor="#FFFFFF50"></up-divider></view>
				<view class="item-buttom">
					<text>{{item.turnoverNumber}}次</text>
					<text>+{{item.cardGrowthRate * 100}}%</text>
				</view>
			</view>

		</view>

		<view class="popup-box">
			<up-popup :show="popupShow" @close="popupShow = false" @open="open">
				<view class="popup">
					<view class="close-box" @click="popupShow = false">
						<image src="/static/images1/close-icon.png" style="width: 26rpx; height: 26rpx;"></image>
					</view>
					<view class="image-box">
						<video :src="'/static/xz/video/'+selectGoods.cardId+'.mp4'" style="width: 520rpx; height: 520rpx;" autoplay="true"
							loop></video>
					</view>
					<view class="title-box" v-if="locale == 'zh'">{{selectGoods.zhName}}</view>
					<view class="title-box" v-if="locale == 'en'">{{selectGoods.enName}}</view>
					<view class="price-box2">
						<image src="/static/images1/star-bb.png" style="width: 42rpx; height: 42rpx;margin-right: 14rpx;"></image>
						{{selectGoods.currentPrice}}
					</view>
					<view class="des-box">
						<text>{{getStr('trade.issuePrice')}}:{{selectGoods.releasePrice}}</text>
						<image src="/static/images1/star-bb.png" style="width: 18rpx; height: 18rpx;margin: 0 6rpx 0 2rpx;"></image>
					</view>
					<view class="des-box">
						<text>{{getStr('trade.lastPrice')}}:{{selectGoods.lastPrice}}</text>
						<image src="/static/images1/star-bb.png" style="width: 18rpx; height: 18rpx;margin: 0 6rpx 0 2rpx;"></image>
					</view>
					<view class="des-box">
						<text>{{selectGoods.turnoverNumber}}次</text>
					</view>
					<view class="des-box">
						<text>+{{selectGoods.cardGrowthRate * 100}}%</text>
					</view>
					<!-- <view class="price-box1">
						<image :src="zsImg" style="width: 28rpx; height: 28rpx;margin-right: 10rpx;"></image>
						+0.024
					</view> -->
					<!-- <view class="btn-box">
						<u-button customStyle="border-radius:12rpx;height:100rpx;font-weight: 500;font-size: 32px;color: #FFFFFF;"
							text="Expired" color="#262930" @click="()=>{}"></u-button>
					</view> -->
				</view>
			</up-popup>
		</view>
	</view>
</template>

<script setup>
	import defaultImg from '@/static/default-img.png'
	import zsImg from '@/static/zs.png'
	import {
		ref
	} from 'vue';
	import {
		onShow,
		onLoad,
		onPullDownRefresh,
		onReachBottom,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		pushAdSellSerarchMyBag
	} from '@/common/api/common.js'
	import {dateFormat} from '@/utils/utils.js'
	import moment from 'moment'

	const source = ref('')
	const cardInfo = ref('')

	const list = ref(['upperFour', 'middleFour', 'lowerFour'])
	// const tabList = ref(['My cards', 'New cards', 'Missed opportunities'])
	const selectSubsection = ref('upperFour')
	// const selectTab = ref('My cards')
	const isRender = ref(false)
	const popupShow = ref(false)
	const selectGoods = ref({})
	const page = ref(1)
	const totalPages = ref(1)

	const goods = ref([])
	
	const locale = ref('en')

	const handleGoods = data => {
		// selectGoods.value = data
		// popupShow.value = true
		if (source.value == 'sell') {
			uni.navigateTo({
				url: '/pages/my/releaseAds/sel?card=' + JSON.stringify(data)
			})
		}
		if (source.value == 'trade') {
			uni.setStorage({
				key: 'tradeCard',
				data: {
					card: data,
					cardInfo: JSON.parse(cardInfo.value)
				},
				success: function() {
					uni.switchTab({
						url: '/pages/trade/index'
					})
				}
			});
		}
		
		if(!source.value){
			selectGoods.value = data
			popupShow.value = true
		}
	}

	const handleSelectSubsection = (data, index) => {
		selectSubsection.value = data
		totalPages.value = 1
		page.value = 1
		getMyBagFn()
	}
	
	const computingTime = (wakeTime) => {
		const currentTime = moment()

		const time24 = moment(wakeTime).add(24,'hours');
		
		if(time24.isAfter(currentTime)){
			const remainingTime = time24.diff(currentTime)
			return remainingTime
		}else{
			return false
		}
	}

	const getMyBagFn = async () => {
		const res = await pushAdSellSerarchMyBag({
			telegramId: uni.getStorageSync('telegramId'),
			page: page.value,
			cardTypeId: list.value.findIndex(item => item == selectSubsection.value) + 1
		})
		if (res.code == 200) {
			if(page.value == 1){
				goods.value = res.data.userCards.map(item=>{
					let data = {
						...item,
						wakeTime: dateFormat(item.wakeTime),
					}
					
					if(item.wakeStatus == 'yes'){
						if(computingTime(item.wakeTime)){
							data.remainingTime = computingTime(item.wakeTime)
						}else{
							data.remainingTime = false
						}
					}
					return data
				})
			}else{
				goods.value = goods.value.concat(res.data.userCards.map(item=>{
					return {
						...item,
						wakeTime: dateFormat(item.wakeTime)
					}
				}))
			}
			
			console.log(goods.value)
			
			totalPages.value = res.data.totalPages
		}
	}

	const getStr = (key) => {
		return uni.$t(key)
	}
	const setIsRender = () => {
		isRender.value = false
		setTimeout(() => {
			isRender.value = true
		}, 50)
	}
	const leftClick = () => {
		uni.switchTab({
			url: '/pages/my/index'
		})
	}

	onLoad((options) => {
		if (options.source) {
			source.value = options.source
			if (options.cardInfo) {
				cardInfo.value = options.cardInfo
			}

		}
		getMyBagFn()
	})
	onShow(() => {
		uni.$tg.showButton()
		setIsRender()
		getMyBagFn()
		locale.value = uni.getStorageSync('locale')
	})
	onReachBottom(() => {
		if (page.value + 1 <= totalPages.value) {
			page.value = page.value + 1
			getMyBagFn()
		}
	})
</script>
<style>
	page {
		background: #1C1E23;
	}
</style>
<style lang="scss" scoped>
	.subsection-box {
		width: calc(100vw - 28rpx - 28rpx);
		height: 84rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
		background: #272A2F;
		border-radius: 12rpx;
		padding: 8rpx;
		box-sizing: border-box;
		margin: 28rpx auto 0;

		.item {
			width: 33.33%;
			height: calc(84rpx - 8rpx - 8rpx);
			color: #ffffff;
			border-radius: 12rpx;
			display: flex;
			justify-content: center;
			align-items: center;
		}

		.active {
			background: #1C1F24;
		}
	}

	.tab-box {
		width: calc(100vw - 28rpx - 28rpx);
		display: flex;
		justify-content: flex-start;
		align-items: center;
		margin: 22rpx auto 0;
		border-bottom: 1px solid #272A2F;

		.item {
			padding: 20rpx 0;
			color: #ffffff;
			display: flex;
			justify-content: center;
			align-items: center;
			font-size: 22rpx;
			margin-right: 30rpx;
			border-bottom: 1px solid #000000;
		}

		.active {
			color: #DCBB4F;
			border-bottom: 1px solid #DCBB4F;
		}
	}

	.list-box {
		padding: 32rpx 28rpx 100rpx;
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;

		.item1 {
			background: linear-gradient(0deg, #25251D 0%, #F7F1CD 100%) !important;
		}
		
		.item2 {
			background: linear-gradient(0deg, #13222D 0%, #5599C0 100%) !important;
		}
		
		.item3 {
			background: linear-gradient(0deg, #271F2A 0%, #D4B6EB 100%) !important;
		}
		
		.item4 {
			background: linear-gradient(0deg, #48282F 0%, #F48A9B 100%) !important;
		}
		.item5 {
			background: linear-gradient(0deg, #21272e 0%, #b7d6ea 100%) !important;
		}
		
		.item6 {
			background: linear-gradient(0deg, #493528 0%, #f3b889 100%) !important;
		}
		
		.item7 {
			background: linear-gradient(0deg, #1C2C21 0%, #b8eab5 100%) !important;
		}
		.item8 {
			background: linear-gradient(0deg, #494728 0%, #f3f48a 100%) !important;
		}
		.item9 {
			background: linear-gradient(0deg, #292320 0%, #ebc2b5 100%) !important;
		}
		.item10 {
			background: linear-gradient(0deg, #2c2848 0%, #9d8af4 100%) !important;
		}
		.item11 {
			background: linear-gradient(0deg, #20212c 0%, #b6c0eb 100%) !important;
		}
		
		.item12 {
			background: linear-gradient(0deg, #3e4828 0%, #c6f48a 100%) !important;
		}
		.item {
			width: 338rpx;
			// height: 398rpx;
			box-sizing: border-box;
			padding-top: 72rpx;
			background: linear-gradient(0deg, #25251D 0%, #F7F1CD 100%);
			border-radius: 32rpx;
			margin-bottom: 18rpx;
			// margin-right: 18rpx;
			position: relative;

			.start-box {
				padding: 0 20rpx;
				height: 42rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				position: absolute;
				right: 0;
				top: 0;
				background: #00000080;
				border-top-right-radius: 32rpx;
				border-bottom-left-radius: 32rpx;
				color: #ffffff;
				font-size: 16rpx;

				::v-deep .u-count-down__text {
					color: #ffffff;
					font-size: 16rpx;
					line-height: 28px;
				}
			}

			.item-info {
				display: flex;
				flex-direction: column;
				align-content: center;

				.title {
					font-weight: 800;
					font-size: 20rpx;
					line-height: 20rpx;
					color: #FFFFFF;
					margin-top: 12rpx;
					text-align: center;
				}

				.des {
					font-weight: 500;
					font-size: 16rpx;
					line-height: 16rpx;
					color: #FFFFFF;
					opacity: 0.5;
					margin-top: 12rpx;
					text-align: center;
				}

				.hour-box {
					font-weight: 500;
					font-size: 14rpx;
					line-height: 14rpx;
					color: #FFFFFF80;
					display: flex;
					justify-content: center;
					align-items: center;
					margin-top: 12rpx;

					text {
						// font-size: 20rpx;
						font-weight: bold;
						font-size: 24rpx;
						color: #FFFFFF;
						margin-right: 4rpx;
					}
				}

				.bottom-box {
					width: calc(100% - 28rpx - 28rpx);
					margin: 20rpx auto 0;
					display: flex;
					justify-content: space-between;
					align-items: center;

					view {
						font-size: 14rpx;
						color: #FFFFFF80;
						display: flex;
						justify-content: flex-start;
						align-items: center;
					}

					text {
						margin-right: 2rpx;
					}
				}

			}

			.divider-box {
				width: calc(100% - 28rpx - 28rpx);
				margin: 10rpx auto 10rpx;

				::v-deep .u-divider {
					margin: 0;
				}
			}

			.item-buttom {
				width: calc(100% - 28rpx - 28rpx);
				margin: 0 auto 30rpx;
				line-height: 20rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-weight: bold;
				font-size: 18rpx;
				color: #FFFFFF;
			}

		}
	}

	.popup-box {

		::v-deep .u-popup__content {
			// height: calc(100vh - 138rpx - 42rpx - 64rpx);
			background: #000000;
			padding-bottom: 42rpx;
			border-top-left-radius: 60rpx;
			border-top-right-radius: 60rpx;
			box-shadow: 0 0 20px rgba(218, 168, 183, 0.6),
				/* 主外发光 */
				0 0 20px rgba(218, 168, 183, 0.2),
				/* 次外发光 */
				0 0 20px rgba(218, 168, 183, 0.1);
			/* 远离中心的外发光 */
			border-top: 1px solid #DAA8B7;
		}

		.close-box {
			width: 26rpx;
			height: 26rpx;
			position: absolute;
			right: 42rpx;
			top: 42rpx;
		}

		.image-box {
			display: flex;
			justify-content: center;
			align-items: center;
			margin-top: 42rpx;
		}

		.title-box {
			font-weight: bold;
			font-size: 48rpx;
			color: #FFFFFF;
			text-align: center;
			margin-top: 30rpx;
			line-height: 48rpx;
		}

		.des-box {
			width: calc(100% - 105rpx - 105rpx);
			margin: 30rpx auto 0;
			font-weight: 500;
			font-size: 24rpx;
			line-height: 26rpx;
			color: #FFFFFF;
			text-align: center;
		}

		.price-box1 {
			display: flex;
			justify-content: center;
			align-items: center;
			opacity: 0.5;
			margin-top: 30rpx;
			font-weight: bold;
			color: #FFFFFF;
			line-height: 20rpx;
		}

		.price-box2 {
			display: flex;
			justify-content: center;
			align-items: center;
			margin-top: 30rpx;
			font-weight: bold;
			font-size: 48rpx;
			line-height: 48rpx;
			color: #FFFFFF;
		}

		.btn-box {
			width: calc(100% - 28rpx - 28rpx);
			margin: 48rpx auto 0;
		}
	}
</style>